import 'dart:math';

import 'package:flutter/material.dart';

class RoomList extends StatefulWidget {
  RoomList({super.key, required this.point, required this.building});

  String point;
  String building;

  @override
  State<RoomList> createState() => _RoomListState();
}

class _RoomListState extends State<RoomList> {
  // 1. 定义初始数据
  Map data = {
    'point': '', // 小区信息
    'building': '', // 楼栋信息
    'rooms': [], // 随机产生的房间号
  };

  @override
  void initState() {
  super.initState();
  fakeData();
  }

  void fakeData() {
  // 1.1 随机产生房间的数量 5-8
  Random random = Random();
  int size = random.nextInt(4) + 5;
  // 1.2 随机产生房间号  102 2002
  List rooms = [];
  for (var i = 0; i < size; i++) {
    // 1.2.1 随机产生楼层 1-20
    int floor = random.nextInt(20) + 1; // 1-20
    // 1.2.2 随机产生房间号 1-4
    int no = random.nextInt(4) + 1;
    // 1.2.3 组合房间号放入数组
    String room = '${floor}0$no';
    if (rooms.contains(room)) continue; // 去重处理
    rooms.add(room);
  }
  // 1.3 更新数据
  setState(() {
    data = {
      'point': widget.point,
      'building': widget.building,
      'rooms': rooms,
    };
  });
}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('选择房间'),
        ),
        body: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: () {
               Navigator.pushNamed(context, '/house_form', arguments: {
              'point': data['point'],
              'building': data['building'],
              'room': data['rooms'][index]
            });
            },
            child: Container(
                color: Colors.white,
                padding: const EdgeInsets.all(10),
                child: Row(children: [
                  Expanded(
                      child: Text(
                          '${data['point']}${data['building']}${data['rooms'][index]}室')),
                  const Row(children: [
                    Icon(Icons.arrow_forward_ios,
                        size: 16, color: Colors.black),
                  ])
                ]))
            )
            ;
          },
          itemCount: (data['rooms'] as List).length,
        ));
  }
}
